//
//  TaskFrequencyShowView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/8.
//

#import "TaskFrequencyShowView.h"

@interface TaskFrequencyShowView ()
@property (nonatomic ,strong) UIView * makeView;
@property (nonatomic ,assign) CGFloat viewH;

@property (nonatomic ,strong) UIView * zdszView;
@property (nonatomic ,strong) UIView * zdszImg;
@property (nonatomic ,strong) UIView * zdszxzImg;


@property (nonatomic ,strong) UIView * bzszView;
@property (nonatomic ,strong) UIView * bzszImg;
@property (nonatomic ,strong) UIView * bzszxzImg;

@property (nonatomic ,strong) BRStringPickerView * pickerView;
@property (nonatomic ,strong) NSMutableArray<BRResultModel *> * dataArray;
@property (nonatomic ,strong) NSArray * index;
@property (nonatomic ,assign) NSInteger taskType;
@property (nonatomic ,strong) void(^block)(NSInteger taskType, NSInteger rateDay, NSInteger rateNumber);

@end

@implementation TaskFrequencyShowView

+ (instancetype)showviewWithType:(NSInteger)type andIndexs:(NSArray *)index andBlock:(void(^)(NSInteger taskType, NSInteger rateDay, NSInteger rateNumber))block{
    TaskFrequencyShowView * view = [TaskFrequencyShowView new];
    view.index = index;
    view.block = block;
    view.taskType = type;
    [view setupview];
    return view;
}


-(NSMutableArray<BRResultModel *> *)dataArray{
    if(!_dataArray){
        NSMutableArray * dataArray = [NSMutableArray array];
        for (int i = 1; i < 61; i++) {
            BRResultModel * rm = [BRResultModel new];
            rm.key = [NSString stringWithFormat:@"F%d",i];
            rm.value = [NSString stringWithFormat:@"%d天",i];
            rm.parentKey = @"-1";
            rm.parentValue = @"";
            rm.remark = [NSString stringWithFormat:@"%d",i];
            [dataArray addObject:rm];
            BRResultModel * dayrm = [BRResultModel new];
            dayrm.key = [NSString stringWithFormat:@"Z%d",i];
            dayrm.value = @"1次";
            dayrm.parentKey = rm.key;
            dayrm.parentValue = rm.value;
            dayrm.remark = [NSString stringWithFormat:@"1"];
            [dataArray addObject:dayrm];
        }
        for (int i = 2; i < 11; i++) {
            BRResultModel * rm = [BRResultModel new];
            rm.key = [NSString stringWithFormat:@"Z%d",i];
            rm.value = [NSString stringWithFormat:@"%d次",i];
            rm.parentKey = @"F1";
            rm.parentValue = @"1";
            rm.remark = [NSString stringWithFormat:@"%d",i];
            
            [dataArray addObject:rm];
        }
        _dataArray  = dataArray;
    }
    return _dataArray;
}
- (void)setupview{
    [[DTKTool getCurrentVC].view addSubview:self];
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.viewH = kwt(500);
    UIView * makeView = [UIView new];
    self.makeView = makeView;
    makeView.backgroundColor = UIColor.whiteColor;
    makeView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH,  self.viewH);
    [self addSubview:makeView];
    [DTKTool BFYJ:kwt(15) andView:makeView andCorner:UIRectCornerTopLeft|UIRectCornerTopRight andSize: CGRectMake(0, 0 ,SCREEN_WIDTH,  self.viewH)];
    
    //
    //    makeView.userInteractionEnabled = YES;
    //    [makeView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(noclick)]];
    //    self.userInteractionEnabled = YES;
    //    [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)]];
    //    makeView.userInteractionEnabled = YES;
    //    [makeView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(noclick)]];
    
    
    UIView * topview = [UIView new];
    [makeView addSubview:topview];
    topview.backgroundColor = KcolorHex(@"#EDF2F8");
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(kwt(52));
    }];
    
    UILabel * toplab = [DTKTool labelWithText:@"任务频次" andFont:kCFont(18) andTextColor:KcolorHex(@"#242729")];
    [topview addSubview:toplab];
    [toplab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.centerY.offset(0);
    }];
    
    UIButton * btn = [UIButton buttonWithType:0];
    [btn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    btn.addTarget(self,@selector(dismiss));
    [topview addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-15));
        make.width.height.offset(kwt(20));
        make.centerY.equalTo(toplab);
    }];
    
    
    UIView * buttonview = [UIView new];
    [makeView addSubview:buttonview];
    [buttonview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(63) + KSafeAreaBottomHeight);
    }];
    
    UIButton * qxbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qxbtn.backgroundColor = KcolorHex(@"#F0F2F4");
    [qxbtn setTitle:@"取消" forState:UIControlStateNormal];
    [qxbtn setTitleColor:KcolorHex(@"#5A6066") forState:UIControlStateNormal];
    qxbtn.titleLabel.font = kCFont(17);
    [DTKTool YJ:kwt(5) andView:qxbtn];
    [qxbtn addTarget:self action:@selector(leftBtnclick) forControlEvents:UIControlEventTouchUpInside];
    [buttonview addSubview:qxbtn];
    [qxbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(44));
        make.width.offset(kwt(160));
        make.left.offset(kwt(20));
        make.top.offset(kwt(10));
    }];
    
    UIButton * qrbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qrbtn.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(kwt(160), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#427FFF") endColor:KcolorHex(@"#3D5FFF")];
    [qrbtn setTitle:@"确定" forState:UIControlStateNormal];
    [qrbtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    qrbtn.titleLabel.font = kCFont(17);
    [DTKTool YJ:kwt(5) andView:qrbtn];
    [qrbtn addTarget:self action:@selector(rightbtnclick) forControlEvents:UIControlEventTouchUpInside];
    [buttonview addSubview:qrbtn];
    [qrbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(44));
        make.width.offset(kwt(160));
        make.right.offset(kwt(-20));
        make.top.offset(kwt(10));
    }];
    
    
    MJWeakSelf
    UIView * zdszView = [UIView new];
    zdszView.addClickBlock(^{
        [weakSelf openZDSZ];
    });
    self.zdszView = zdszView;
    [makeView addSubview:zdszView];
    [DTKTool YJBK:kwt(5) andView:zdszView andWidth:0.5 andColor:KcolorHex(@"#D8D8D8")];
    zdszView.backgroundColor = KcolorHex(@"#FBFDFF");
    [zdszView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.right.offset(kwt(-15));
        make.top.equalTo(topview.mas_bottom).offset(kwt(15));
        make.height.offset(kwt(50));
    }];
    
    UILabel * zdlab  = [DTKTool labelWithText:@"自定设置" andFont:kCFont(18) andTextColor:KcolorHex(@"#242729")];
    [zdszView addSubview:zdlab];
    [zdlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.centerY.offset(0);
    }];
    
    UILabel * zdlab1  = [DTKTool labelWithText:@"(根据情况动态添加)" andFont:kFont(18) andTextColor:KcolorHex(@"#ACACAC")];
    [zdszView addSubview:zdlab1];
    [zdlab1  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(zdlab.mas_right);
        make.centerY.offset(0);
    }];
    
    UIView * zdszImg = [UIView new];;
    zdszImg.backgroundColor = UIColor.whiteColor;
    self.zdszImg = zdszImg;
    [DTKTool YJBK:kwt(9) andView:zdszImg andWidth:1 andColor:KcolorHex(@"#C0C0C0")];
    [zdszView addSubview:zdszImg];
    [zdszImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-15));
        make.width.height.offset(kwt(18));
        make.centerY.offset(0);
    }];
    
    UIView * zdszxzImg = [UIView new];
    zdszxzImg.backgroundColor = UIColor.whiteColor;
    self.zdszxzImg = zdszxzImg;
    [DTKTool YJ:kwt(5) andView:zdszxzImg];
    [zdszImg addSubview:zdszxzImg];
    [zdszxzImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(10));
        make.center.offset(0);
    }];
    
    
    UIView * bzBjview = [UIView new];
    [DTKTool YJBK:kwt(5) andView:bzBjview andWidth:0.5 andColor:KcolorHex(@"#EAEAEA")];
    bzBjview.backgroundColor = KcolorHex(@"#FBFDFF");
    [makeView addSubview:bzBjview];
    [bzBjview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.right.offset(kwt(-15));
        make.top.equalTo(zdszView.mas_bottom).offset(kwt(10));
        make.bottom.equalTo(buttonview.mas_top).offset(kwt(-10));
    }];
    
    UIView * bzszView = [UIView new];
    bzszView.addClickBlock(^{
        [weakSelf openBZSZ];
    });
    self.bzszView = bzszView;
    [makeView addSubview:bzszView];
    [DTKTool YJBK:kwt(5) andView:bzszView andWidth:0.5 andColor:KcolorHex(@"#D8D8D8")];
    bzszView.backgroundColor = KcolorHex(@"#FBFDFF");
    [bzszView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(bzBjview);
        make.height.offset(kwt(50));
    }];
    
    UILabel * bzlab  = [DTKTool labelWithText:@"标准设置" andFont:kCFont(18) andTextColor:KcolorHex(@"#242729")];
    [bzszView addSubview:bzlab];
    [bzlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.centerY.offset(0);
    }];
    
    UILabel * bzlab1  = [DTKTool labelWithText:@"(1天X次，或X天1次)" andFont:kFont(18) andTextColor:KcolorHex(@"#ACACAC")];
    [bzszView addSubview:bzlab1];
    [bzlab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bzlab.mas_right);
        make.centerY.offset(0);
    }];
    
    UIView * bzszImg = [UIView new];;
    bzszImg.backgroundColor = UIColor.whiteColor;
    self.bzszImg = bzszImg;
    [DTKTool YJBK:kwt(9) andView:bzszImg andWidth:1 andColor:KcolorHex(@"#C0C0C0")];
    [bzszView addSubview:bzszImg];
    [bzszImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-15));
        make.width.height.offset(kwt(18));
        make.centerY.offset(0);
    }];
    
    UIView * bzszxzImg = [UIView new];
    bzszxzImg.backgroundColor = UIColor.whiteColor;
    self.bzszxzImg = bzszxzImg;
    [DTKTool YJ:kwt(5) andView:bzszxzImg];
    [bzszImg addSubview:bzszxzImg];
    [bzszxzImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(10));
        make.center.offset(0);
    }];
    
    UIView * pickview = [UIView new];
    [bzBjview addSubview:pickview];
    [pickview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(bzszView.mas_bottom).offset(kwt(10));
        make.bottom.offset(kwt(-10));
    }];
    
    
    BRStringPickerView * pickerView = [[BRStringPickerView alloc] initWithPickerMode:BRStringPickerComponentLinkage];
    BRPickerStyle * style = [BRPickerStyle new];
    style.hiddenMaskView = YES;
    style.pickerColor = UIColor.clearColor;
    style.selectRowColor = KcolorHex(@"#EAEFF8");
    style.separatorColor = KcolorHex(@"#EAEFF8");
    style.selectRowTextColor = KcolorHex(@"#0967FF");
    style.pickerTextFont = kFont(17);
    style.pickerTextColor = KcolorHex(@"#242729");
    pickerView.pickerStyle = style;
    self.pickerView = pickerView;
    self.pickerView.dataSourceArr = self.dataArray;

    self.pickerView.selectIndexs = self.index;
    
    //    pickerView.numberOfComponents = 3;
    pickerView.resultModelArrayBlock = ^(NSArray<BRResultModel *> * _Nullable resultModelArr) {
        if(weakSelf.block){
            weakSelf.block(weakSelf.taskType,resultModelArr.firstObject.value.integerValue ,resultModelArr.lastObject.value.integerValue);
            [weakSelf dismiss];
        }
    };
    [pickerView addPickerToView:pickview];
    
    if(weakSelf.taskType == 2){
        [self openZDSZ];
    } else if(weakSelf.taskType == 1){
        [self openBZSZ];
    }
    [self show];
}
- (void)openZDSZ{
    self.taskType = 2;
    self.zdszView.backgroundColor = KcolorHex(@"#F5FAFF");
    [DTKTool YJBK:kwt(5) andView:self.zdszView andWidth:0.5 andColor:KcolorHex(@"#0967FF")];
    [DTKTool YJBK:kwt(9) andView:self.zdszView andWidth:1 andColor:KcolorHex(@"#0967FF")];
    self.zdszxzImg.backgroundColor = KcolorHex(@"#0967FF");
    
    self.bzszView.backgroundColor = KcolorHex(@"#FBFDFF");
    [DTKTool YJBK:kwt(5) andView:self.bzszView andWidth:0.5 andColor:KcolorHex(@"#D8D8D8")];
    [DTKTool YJBK:kwt(9) andView:self.bzszImg andWidth:1 andColor:KcolorHex(@"#C0C0C0")];
    self.bzszxzImg.backgroundColor = UIColor.whiteColor;
    
}
- (void)openBZSZ{
    self.taskType = 1;
    self.bzszView.backgroundColor = KcolorHex(@"#F5FAFF");
    [DTKTool YJBK:kwt(5) andView:self.bzszView andWidth:0.5 andColor:KcolorHex(@"#0967FF")];
    [DTKTool YJBK:kwt(9) andView:self.bzszImg andWidth:1 andColor:KcolorHex(@"#0967FF")];
    self.bzszxzImg.backgroundColor = KcolorHex(@"#0967FF");
    
    self.zdszView.backgroundColor = KcolorHex(@"#FBFDFF");
    [DTKTool YJBK:kwt(5) andView:self.zdszView andWidth:0.5 andColor:KcolorHex(@"#D8D8D8")];
    [DTKTool YJBK:kwt(9) andView:self.zdszView andWidth:1 andColor:KcolorHex(@"#C0C0C0")];
    self.zdszxzImg.backgroundColor = UIColor.whiteColor;
}
- (void)leftBtnclick{
    [self dismiss];
    
}

- (void)rightbtnclick{
    if(self.taskType == 1){
        self.pickerView.doneBlock();
    } else {
        self.block(2, 1, 1);
        [self dismiss];
    }
 
}


- (void)show{
    [[DTKTool getCurrentVC].view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT - self.viewH , SCREEN_WIDTH, self.viewH);
    } completion:^(BOOL finished) {
    }];
}
- (void)noclick{}
- (void)dismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT , SCREEN_WIDTH, self.viewH);
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}
@end
